package com.jmlong.controller;

import com.jmlong.domain.Book;
import com.jmlong.service.BookService;
import com.jmlong.service.CommodityService;
import com.jmlong.utils.Result;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.swing.plaf.PanelUI;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    CommodityService commodityService;

    @Autowired
    BookService bookService;

    /**
     * 用户获取商品总价格和总数量
     */
    @PostMapping("/getPriceAndCount")
    public Result getPriceAndCount(@RequestBody Map params){
        log.info("用户购物车商品信息查询中！");
        Result result = new Result();
        JSONArray records = bookService.getPriceAndCount(params);
        result.setData(records);
        log.info("查询完毕");
        return result;
    }

    /**
     * 用户提交订单
     */
    @PostMapping("/submitBook")
    public Result submitBook(ServletRequest servletRequest,@RequestBody Map params){
        log.info("用户提交订单.....");
        Result result = new Result();
        String loginName = (String) servletRequest.getAttribute("loginName");

        JSONObject record = bookService.submitBook(loginName, params);
        if (record!=null){
            log.info("用户提交订单成功！");
            result.setMsg("提交订单成功！");
            result.setData(record);
        }else{
            log.info("用户提交订单失败！");
            result.setMsg("提交订单失败！");
        }

        return result;
    }

    /**
     * 用户取消订单
     */
    @PostMapping("/cancelBook")
    public Result cancelBook(@RequestBody Map params){
        log.info("用户请求取消订单...");
        Result result = new Result();
        Integer res = bookService.cancelBook(params);
        if (res!=0){
            log.info("用户请求取消订单成功！");
            result.setMsg("删除成功！");
        }else{
            log.info("用户请求取消订单失败！");
            result.setCode(500);
            result.setMsg("删除失败！");
        }

        return result;
    }

    /**
     * 用户确认付款
     */
    @PostMapping("/payment")
    public Result payment(@RequestBody Map params){
        log.info("用户请求付款....");
        return bookService.payment(params);
    }

    /**
     * 用户请求订单信息
     */
    @GetMapping("/getBook")
    public Result getBook(ServletRequest servletRequest,String status){
        log.info("用户请求订单信息....");
        Result result = new Result();
        String loginName = (String) servletRequest.getAttribute("loginName");
        List<Book> books = bookService.getBook(loginName,status);
        if (books!=null && books.size()>0){
            log.info("用户请求订单信息成功！");
            result.setData(books);
            result.setMsg("查询订单信息成功");
        }else if(books!=null && books.size()==0) {
            log.info("用户请求订单信息为空！");
            result.setMsg("暂无订单信息");
        }else{
            log.info("用户请求订单信息失败！");
            result.setCode(500);
            result.setMsg("查询订单信息失败！");
        }

        return result;
    }

    /**
     * 用户请求订单明细
     */
    @PostMapping("/getBookInfo")
    public Result getBookItem(@RequestBody Map params){
        log.info("用户查询订单明细...");
        Result result = new Result();
        JSONObject records = bookService.getBookItem(params);
        if (records!=null){
            log.info("查询成功！");
            result.setData(records);
        }else {
            log.info("查询失败！");
            result.setCode(500);
            result.setMsg("查询订单失败！");
        }
        return  result;
    }

    //商家获取订单信息
    @GetMapping("/getMerchantBook")
    public Result getMerchantBook(ServletRequest servletRequest){
        log.info("商家请求订单信息....");
        Result result = new Result();
        String loginName = (String) servletRequest.getAttribute("loginName");
        JSONArray records = bookService.getMerchantBook(loginName);
        if (records.size()>0){
            log.info("查询完毕！");
            result.setMsg("查询订单成功!");
            result.setData(records);
        }else if (records.size()==0){
            log.info("查询订单信息为空！");
            result.setMsg("暂无商家订单信息！");
        }else {

            log.info("查询订单信息失败！");
            result.setCode(500);
            result.setMsg("查询订单信息失败！");
        }

        return result;
    }

    /**
     * 商家更改订单状态
     */
    @PostMapping("/shipment")
    public Result shipment(@RequestBody Map params){
        log.info("商家请求商品发货操作....");
        Result result = new Result();
        Integer res = bookService.shipment(params);
        if (res!=0){
            log.info("操作成功！");
            result.setMsg("操作成功！");
        }else{
            result.setCode(500);
            result.setMsg("操作失败！");
        }

        return result;
    }

    /**
     * 用户请求代发货数据
     */
    @GetMapping("/userShipment")
    public Result userShipment(ServletRequest servletRequest){
        log.info("用户查询代发货信息....");
        Result result = new Result();
        String loginName = (String) servletRequest.getAttribute("loginName");
        JSONArray records = bookService.userShipment(loginName);
        if (records.size()>0){
            log.info("查询完毕！");
            result.setMsg("查询订单成功!");
            result.setData(records);
        }else if (records.size()==0){
            log.info("查询订单信息为空！");
            result.setMsg("暂无用户订单信息！");
        }else {

            log.info("查询订单信息失败！");
            result.setCode(500);
            result.setMsg("查询订单信息失败！");
        }

        return  result;
    }

    /**
     * 用户请求代收货数据
     */
    @GetMapping("/userBookHarvest")
    public Result userBookHarvest(ServletRequest servletRequest){
        log.info("用户查询代发货信息....");
        Result result = new Result();
        String loginName = (String) servletRequest.getAttribute("loginName");
        JSONArray records = bookService.userBookHarvest(loginName);
        if (records.size()>0){
            log.info("查询完毕！");
            result.setMsg("查询订单成功!");
            result.setData(records);
        }else if (records.size()==0){
            log.info("查询订单信息为空！");
            result.setMsg("暂无用户订单信息！");
        }else {

            log.info("查询订单信息失败！");
            result.setCode(500);
            result.setMsg("查询订单信息失败！");
        }

        return result;
    }

    /**
     * 用户请求收货操作
     */
    @PostMapping("/userConfirmBookHarvest")
    public Result userConfirmBookHarvest(@RequestBody Map params){
        log.info("用户确认收货....");
        Result result = new Result();
        Integer res = bookService.userConfirmBookHarvest(params);
        if (res!=0){
            log.info("收货成功！");
            result.setMsg("收货成功！");
        }else{
            log.info("收货失败！");
            result.setCode(500);
            result.setMsg("收货失败！");
        }
        return result;
    }

    /**
     * 用户请求已收货数据
     */
    @GetMapping("/userFinishBook")
    public Result userFinishBook(ServletRequest servletRequest){
        log.info("用户请求已完成订单信息....");
        Result result = new Result();
        String loginName = (String) servletRequest.getAttribute("loginName");
        JSONArray records = bookService.userFinishBook(loginName);
        if (records.size()>0){
            log.info("查询完毕！");
            result.setMsg("查询订单成功!");
            result.setData(records);
        }else if (records.size()==0){
            log.info("查询订单信息为空！");
            result.setMsg("暂无用户订单信息！");
        }else {

            log.info("查询订单信息失败！");
            result.setCode(500);
            result.setMsg("查询订单信息失败！");
        }

        return result;
    }

    /**
     * 用户查询状态
     */
    @GetMapping("/userBookStatus")
    public Result userBookStatus(ServletRequest servletRequest,String status){
        log.info("用户查询订单状态....");
        Result result = new Result();
        String loginName = (String) servletRequest.getAttribute("loginName");
        if (status==null||status.equals("")){
            log.info("用户查询失败！");
            result.setCode(500);
            result.setMsg("查询订单信息失败！");
        }else{
            JSONArray records = bookService.userBookStatus(loginName, status);
            if (records.size()>0){
                log.info("查询完毕！");
                result.setMsg("查询订单成功!");
                result.setData(records);
            }else if (records.size()==0){
                log.info("查询订单信息为空！");
                result.setMsg("暂无用户订单信息！");
            }else {

                log.info("查询订单信息失败！");
                result.setCode(500);
                result.setMsg("查询订单信息失败！");
            }
        }

        return result;
    }


}
